﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataCom;
using ConfigurationFactory;
using System.Data;

namespace DBAccessLib
{
    public abstract class Product
    {
        public static DataTable GetAllLines()
        {
            ILineNameAccess access = DataCommunicationFactory.GetLineNameAccess();
            return access.GetAllLineName();
        }

        private static bool GetDateAndShift(string lineCode, out string date, out string shift)
        {
            DateTime dt = DateTime.Now;
            date = dt.Date.ToString("yyyy-MM-dd");
            string time = dt.ToString("HH:mm:ss");
            shift = "1";
            ILineWatchSysAccess access = DataCommunicationFactory.GetLineWatchSysAccess();
            DataRow row = access.GetRecordByLineCodeAndShift(lineCode, "ShiftB");
            if (row == null) return false;

            string lower = row["CONTENT2"].ToString().Trim();
            string upper = row["CONTENT1"].ToString().Trim();
            if (time.CompareTo(lower) < 0)
            {
                date = dt.AddDays(-1).Date.ToString("yyyy-MM-dd");
                shift = "2";
            }
            else if (time.CompareTo(upper) > 0)
            {
                shift = "2";
            }
            return true;
        }

        public static DataRow GetProductInfo(string lineCode)
        {
            string date, shift;
            if (!GetDateAndShift(lineCode, out date, out shift)) return null;

            IProductLogAccess productAccess = DataCommunicationFactory.GetProductLogAccess();
            DataRow rowProduct = productAccess.GetProductLogByDateLineAndShift(date, lineCode, shift);
            return rowProduct;
        }

        public static DataRow GetLineWatchRecord(string lineCode, string function)
        {
            ILineWatchSysAccess access = DataCommunicationFactory.GetLineWatchSysAccess();
            DataRow row = access.GetRecordByLineCodeAndShift(lineCode, function);
            return row;
        }

        public static List<string> GetHelpPositionInfo(string lineCode)
        {
            List<string> li = new List<string>();
            string date, shift;
            if (!GetDateAndShift(lineCode, out date, out shift)) return li;

            IPositionAccess access = DataCommunicationFactory.GetPositionAccess();
            DataTable dt = access.GetPositions(lineCode, date, shift, PositionType.H);
            if (dt == null) return li;
            foreach (DataRow row in dt.Rows)
            {
                li.Add(row["FPOSITIONID"].ToString() + " " + row["Position"].ToString());
            }
            return li;
        }

        public static List<string> GetStopPositionInfo(string lineCode)
        {
            List<string> li = new List<string>();
            string date, shift;
            if (!GetDateAndShift(lineCode, out date, out shift)) return li;

            IPositionAccess access = DataCommunicationFactory.GetPositionAccess();
            DataTable dt = access.GetPositions(lineCode, date, shift, PositionType.S);
            if (dt == null) return li;
            foreach (DataRow row in dt.Rows)
            {
                li.Add(row["FPOSITIONID"].ToString() + " " + row["Position"].ToString());
            }
            return li;
        }

        public static List<string> GetAlarmPositionInfo(string lineCode)
        {
            List<string> li = new List<string>();
            string date, shift;
            if (!GetDateAndShift(lineCode, out date, out shift)) return li;

            IPositionAccess access = DataCommunicationFactory.GetPositionAccess();
            DataTable dt = access.GetPositions(lineCode, date, shift, PositionType.A);
            if (dt == null) return li;
            foreach (DataRow row in dt.Rows)
            {
                li.Add(row["FPOSITIONID"].ToString() + " " + row["Position"].ToString());
            }
            return li;
        }

        public static DataTable GetStartSummary(string from, string to, string line, string shift, string position)
        {
            IPositionLogAccess positionLogAccess = DataCommunicationFactory.GetPositionLogAccess();
            DataTable positionSummary = positionLogAccess.GetPositionLogSummary(from, to, line, shift, position);
            return positionSummary;
        }

        public static DataTable GetProduct(string from, string to, string line, string shift)
        {
            IProductLogAccess productAccess = DataCommunicationFactory.GetProductLogAccess();
            DataTable dt = productAccess.GetProductLog(from, to, line, shift);
            return dt;
        }

        public static DataTable GetStart(string from, string to, string line, string shift, string position)
        {
            IPositionLogAccess positionLogAccess = DataCommunicationFactory.GetPositionLogAccess();
            DataTable positionSummary = positionLogAccess.GetPositionLog(from, to, line, shift, position);
            return positionSummary;
        }

        public static void UpdateProductLogByKey(string lineCode,
                                                 string logDate,
                                                 string shift,
                                                 string planNum,
                                                 string nowPlan,
                                                 string productNum,
                                                 string stopTime)
        {
            IProductLogAccess productAccess = DataCommunicationFactory.GetProductLogAccess();
            productAccess.UpdateProductLogByKey(lineCode, logDate, shift, planNum, nowPlan, productNum, stopTime);
        }
    }
}
